import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";

@Injectable({
  providedIn: 'root'
})
export class GetDataService {
  // token 数据
  xCsrftoken: string;
  __formHead: HttpHeaders;
  __formHead2: HttpHeaders;
  __formHead3: HttpHeaders;
  constructor(private http: HttpClient) {
    // let token = this.getLocalToken();
    // if (token == null) throw "No token";
    // headers 头部可以使用链式调用和赋值 分开写可能会失效
    this.__formHead = new HttpHeaders().append('Content-Type', 'application/x-www-form-urlencoded');//.append('x-csrftoken', token);
    this.__formHead2 = new HttpHeaders({'Content-Type': 'multipart/form-data'});
    this.__formHead3 = new HttpHeaders({'Content-Type': 'application/json'});
  }
  // 获取TA列表
  getTaList() {
    return this.http.get('/uniform-api/flow-tas/', {headers: this.__formHead});
  }
  // 创建申请
  createApprove(objs) {
    return this.http.post('/uniform-api/flow-requests/', objs, {headers: this.__formHead3});
  }
  // 获取我的历史
  getHistory(queryStr) {
    let url = encodeURI('/uniform-api/flow-requests/my-history/?'+queryStr);
    return this.http.get(url);
  }
  // 获取需要我审批的申请列表
  getApprove(queryStr) {
    let url = encodeURI('/uniform-api//flow-requests/need-approve/?'+queryStr);
    return this.http.get(url);
  }
  // 审批
  goApprove(id, type, objs) {
    // type advise reject approve
    return this.http.post(`/uniform-api/flow-requests/${id}/${type}/`, objs,  {headers: this.__formHead3});
  }
  // 修改申请后重新提交申请
  anewApprove(id, objs) {
    return this.http.patch(`/uniform-api/flow-requests/${id}/fix-and-resubmit/`, objs, {headers: this.__formHead3});
  }
  // 获取我的草稿列表
  getDraftList(queryStr) {
    let url = encodeURI('/uniform-api/flow-request-drafts/?'+queryStr);
    return this.http.get(url);
  }
  // 获取具体的某个草稿
  getDraft(id) {
    let url = encodeURI(`/uniform-api/flow-request-drafts/${id}/`);
    return this.http.get(url);
  }
  // 保存为草稿
  saveDrafts(objs) {
    return this.http.post(`/uniform-api/flow-request-drafts/`, objs, {headers: this.__formHead3});
  }
  // 修改草稿
  modifyDraft(id, objs) {
    return this.http.patch(`/uniform-api/flow-request-drafts/${id}`, objs, {headers: this.__formHead3});
  }
  // 删除具体的某个草稿
  deleteDraft(id) {
    let url = encodeURI(`/uniform-api/flow-request-drafts/${id}/`);
    return this.http.delete(url, {headers: this.__formHead3});
  }
  /* 用户模块 */
  // 获取用户列表
  getUserList(queryStr) {
    let url = encodeURI('/uniform-api/flow-users/?'+queryStr);
    return this.http.get(url);
  }
  // 创建一个用户
  addAccount(objs) {
    return this.http.post(`/uniform-api/flow-users/`, objs, {headers: this.__formHead3});
  }
  // 获取具体的某个用户
  getUser(id) {
    let url = encodeURI(`/uniform-api/flow-users/${id}/`);
    return this.http.get(url);
  }
  // 修改用户
  modifyUser(id, objs) {
    return this.http.patch(`/uniform-api/flow-users/${id}`, objs, {headers: this.__formHead3});
  }
  // 删除具体的某个用户
  deleteUser(id) {
    let url = encodeURI(`/uniform-api/flow-users/${id}/`);
    return this.http.delete(url, {headers: this.__formHead3});
  }
  /* 数据模块 */
  getImportDataList(queryStr) {
    let url = encodeURI('/uniform-api/data-import-job/?'+queryStr);
    return this.http.get(url);
  }
  // 创建一个导入任务
  createImportTask(objs) {
    return this.http.post('/uniform-api/data-import-job/', objs, {headers: this.__formHead3});
  }
  getExportDataList(queryStr) {
    let url = encodeURI('/uniform-api/data-export-job/?'+queryStr);
    return this.http.get(url);
  }
  // 创建一个导出任务
  createExportTask(objs) {
    return this.http.post('/uniform-api/data-export-job/', objs, {headers: this.__formHead3});
  }
  // 下载导出任务
  downloadExportTask(id) {
    return this.http.get(`/uniform-api/data-export-job/${id}/download-results/`);
  }
  /* 凭证获取 */
  // 获取上传文件凭证
  getPreVoucher() {
    return this.http.get('/uniform-api/flow-materials-upload/my_credential_entity/',  {headers: this.__formHead3});
  }


  // 获取token
  private getLocalToken(): string {
    if (!this.xCsrftoken) {
      this.xCsrftoken = sessionStorage.getItem('xCsrftoken');
    }
    return this.xCsrftoken;
  }
}
